package cn.jinjimi.app.main.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import cn.jinjimi.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 弹出菜单通用dialog
 *
 * Created by jinsen on 15/11/9.
 */
public class CommMenuDialog extends Dialog{

    Context mContext;

    TextView mTitleText;
    ImageView mTitleBarLine;
    ListView mMenuList;

    ArrayList<String> mMenuName = null;

    MenuAdapter mAdapter;
    int mMenuItemHeight = 0;

    public CommMenuDialog(Context context) {
        this(context, R.style.TransparentDialog);
    }

    public CommMenuDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        setContentView(R.layout.dialog_menu_layout);
        mTitleText = (TextView) findViewById(R.id.menu_title_text);
        mTitleBarLine = (ImageView) findViewById(R.id.menu_title_line);
        mMenuList  = (ListView) findViewById(R.id.menu_list_action_listview);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        mMenuItemHeight = mContext.getResources().getDimensionPixelSize(R.dimen.dp_55);

        mMenuName = new ArrayList<String>();
        mAdapter = new MenuAdapter();
        mMenuList.setAdapter(mAdapter);

        hideTitle();
    }


    public void setMenu(List<String> menu){
        mMenuName.clear();
        mMenuName.addAll(menu);
        mAdapter.notifyDataSetChanged();
    }

    public void setMenuItemLisener(AdapterView.OnItemClickListener listener){
        mMenuList.setOnItemClickListener(listener);
    }

    public void setTitle(String title){
        mTitleText.setVisibility(View.VISIBLE);
        mTitleBarLine.setVisibility(View.VISIBLE);
        mTitleText.setText(title);
    }

    public void hideTitle(){
        mTitleText.setVisibility(View.GONE);
        mTitleBarLine.setVisibility(View.GONE);
    }


    class MenuAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mMenuName.size();
        }

        @Override
        public Object getItem(int position) {
            return mMenuName.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = new TextView(mContext);
                convertView.setPadding(mContext.getResources().getDimensionPixelSize(R.dimen.dp_10),0,mContext.getResources().getDimensionPixelSize(R.dimen.dp_10),0);
                ((TextView)convertView).setTextColor(Color.BLACK);
                ((TextView)convertView).setGravity(Gravity.CENTER_VERTICAL);
                ((TextView)convertView).setTextSize(15);
                AbsListView.LayoutParams alp = new AbsListView.LayoutParams(-1,mMenuItemHeight);
                convertView.setLayoutParams(alp);
            }

            ((TextView)convertView).setText(mMenuName.get(position));

            return convertView;
        }
    }


}
